﻿/*
 * Copyright (C) 图无形 - All Rights Reserved
 *
 * 未经作者授权，请勿分发、修改本文件。
 */
using AnyCAD.Foundation;
using AnyCAD.NX.ViewModel;
using AnyDraw.Nest;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Fluent;
using System.Diagnostics;

namespace AnyDraw
{
    internal partial class MainViewModelImpl : MainRibbonViewModel
    {
        public MainViewModelImpl(IRenderView viewer)
            : base(viewer)
        {
            this.FileFilter = "AnyDraw Files (*.draw;*.acad)|*.draw;*.acad";
            this.DefaultExt = ".draw";
        }

        public override Ribbon GetRibbonBar()
        {
            return ((MainWindow)App.Current.MainWindow).mRibbon;
        }

        public void SetTitle(string title)
        {
            ((MainWindow)App.Current.MainWindow).Title = title;
        }

        protected override void DoInitialize()
        {
            base.DoInitialize();            
            Viewer.SetStandardView(EnumStandardView.Top, false);
            Viewer.SetViewMode2D(true);

            var cws = new CoordinateWidgetSetting(Viewer);
            cws.SetCoordinateWidget(EnumViewCoordinateType.Axis);
            cws.SetCoordinateWidgetText("x", "y", "");       

            var rs = new RulerWidgetSetting(Viewer);
            rs.SetRulerWidget(EnumRulerWidgetType.Default);
            rs.SetNumberColor(ColorTable.LightGray);
            rs.SetMarkColor(ColorTable.LightGray);

            ViewContext.SetOrbitButton(EnumMouseButton.Zero);
            Viewer.SetBackgroundColor(new Vector4(33 / 255.0f, 40 / 255.0f, 48 / 255.0f, 0));
            var material = ViewContext.GetDefaultMaterial(EnumShapeFilter.Edge);
            material.SetColor(ColorTable.White);
        }

        protected override void OnImportModel()
        {
            var dlg = new Microsoft.Win32.OpenFileDialog
            {
                DefaultExt = ".dxf",
                Filter = "DXF Files (*.dxf;*.json)|*.dxf;*.json"
            };
            var result = dlg.ShowDialog();
            if (result != true)
                return;
            var doc = Document;
            var undo = new UndoTransaction(doc);
            undo.Start("Import");
            if (dlg.FileName.EndsWith(".dxf"))
            {
                var dxf = ShapeIO.Open(dlg.FileName);
                if(dxf != null )
                {
                    var shapes = dxf.GetChildren(EnumTopoShapeType.Topo_EDGE);
                    for (int ii=0; ii<shapes.Count; ++ii)
                    {
                        var shape = shapes[ii];
                        if(shape == null) 
                            continue;
                        if(shape.GetShapeType() == EnumTopoShapeType.Topo_EDGE)
                        {
                            var sce = SimpleCurveElement.Create(doc);
                            sce.SetCurve(shape);
                        }
                        else
                        {
                            var sp = ShapeElement.Create(doc);
                            sp.SetShape(shape);
                        }
                    }
                }

            }
            else
            {
                var db = AnyCAD.IO.Drawing.DrawingDb.Load(dlg.FileName);
                db?.Show(doc);
            }
            undo.Commit();

            //ZoomFitCommand.Execute("");
        }

        [RelayCommand]
        void Nest()
        {
            var view = new NestConfigView(Document);
            view.ShowDialog();
            StatusMessage = view.Message;
        }


        public bool IsCheckedCommandPoint { get => IsCommandChecked("Point"); }
        public bool IsCheckedCommandLine { get => IsCommandChecked("Line"); }
        public bool IsCheckedCommandPolyline { get => IsCommandChecked("Polyline"); }
        public bool IsCheckedCommandRectangle { get => IsCommandChecked("Rectangle"); }
        public bool IsCheckedCommandArc { get => IsCommandChecked("Arc"); }
        public bool IsCheckedCommandCircle { get => IsCommandChecked("Circle"); }
        public bool IsCheckedCommandModifyObject { get => IsCommandChecked("ModifyObject"); }


        public bool IsEnabledCommandBySelection { get
            {
                var doc = Document;
                if (doc == null)
                    return false;
                return doc.GetSelection().Count > 0;
            } }

    }
}
